package com.zy.smbms.Filter;
//登陆过滤器

import com.zy.smbms.pojo.SmbmsUser;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter implements Filter
{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
    {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        String requestURI = request.getRequestURI();
        SmbmsUser user=(SmbmsUser) request.getSession().getAttribute("sessionUser");
        if(requestURI.contains("checkLogin") || requestURI.contains("/css/") || requestURI.contains("/images/") ||requestURI.contains("/js/"))
        {
            //排除掉包含的登录页面及资源
            filterChain.doFilter(servletRequest,servletResponse);
        }
        else if(user!=null)
        {
            //登陆成功
            filterChain.doFilter(servletRequest,servletResponse);
        }
        else
        {
            request.getRequestDispatcher("/userController/login").forward(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy()
    {

    }
}
